package com.nwpu.am.controller.staff;

import com.nwpu.am.exception.BizException;
import com.nwpu.am.pojo.dto.OrderCreateDTO;
import com.nwpu.am.pojo.dto.page.OrderPageDTO;
import com.nwpu.am.pojo.entity.Order;
import com.nwpu.am.result.PageResult;
import com.nwpu.am.result.Result;
import com.nwpu.am.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * StaffController
 *
 * @author Roy
 * @version 1.0
 * {@code @date} 2024/6/5
 */

@RestController
@RequestMapping("/staff")
@Slf4j
@Api(tags = "修理人员接口")
public class StaffController {
    @Resource
    OrderService orderService;

    @PostMapping("/createOrder")
    @ApiOperation("创建订单")
    public Result<String> createOrder(@RequestBody @Validated OrderCreateDTO order) throws BizException {
       try {
            orderService.createOrder(order);
       } catch (Exception e) {
           throw new BizException("订单创建失败");
        }

        return Result.success("订单创建成功");
    }

    @PostMapping("/displayOrderList")
    @ApiOperation("展示订单列表")
    public Result<PageResult<Order>> displayOrderList(OrderPageDTO orderPageDTO) {
        log.info("维修订单分页查询，参数为：{}", orderPageDTO);
        PageResult<Order> pageResult = orderService.pageQuery(orderPageDTO);
        return Result.success(pageResult);
    }

    @PostMapping("/updateOrder/{orderId}")
    @ApiOperation("修改订单状态")
    public Result<String> updateOrder(@RequestBody @Validated OrderCreateDTO order,@PathVariable("orderId")Long orderId) throws BizException{
        try {
            orderService.updateOrder(order,orderId);
        } catch (Exception e) {
            log.info(e.getMessage());
            throw new BizException("订单更新失败");
        }

        return Result.success("订单更新成功");
    }
    @PostMapping("/orderFinished/{orderId}")
    @ApiOperation("修改订单状态为结束")
    public void orderFinished(@PathVariable("orderId")Long orderId){
        orderService.orderFinished(orderId);
    }
    @GetMapping ("/searchDetails/{id}")
    @ApiOperation("查看订单详情")
    public Order searchDetails(@PathVariable("id") Long orderId){
         return orderService.findById(orderId);




    }

    @GetMapping("/deleteOrder/{orderId}")
    @ApiOperation("删除订单")
    public void deleteOrderById(@PathVariable("orderId") Long orderId){
         orderService.deleteOrder(orderId);




    }






}
